package org.clockworkmages.games.anno1186.model.option;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import org.clockworkmages.games.anno1186.GameBeansContext;
import org.clockworkmages.games.anno1186.dao.GameObjectsList;
import org.clockworkmages.games.anno1186.model.Effecting;
import org.clockworkmages.games.anno1186.model.effect.Effect;
import org.clockworkmages.games.anno1186.scripting.ScriptingService;

public class GenericOption
		extends Option implements Effecting {

	/**
	 * Reference to the ID of a named Option. If this value is set, the Option
	 * is treated as a virtual markup to be removed and replaced by a reference
	 * to the named option during game data loading process.
	 */
	private String ref;

	/**
	 * The text to be added to the "You see..." portion of the Situation's
	 * description text.
	 */
	private String see;

	/**
	 * Id of the Situation that this option should be appended to. This field
	 * should only be set for a "located" options - GenericOption defined at the
	 * top-level of a ${link {@link GameObjectsList} that is supposed to extend
	 * a Situation defined in a different file (which makes modding easier).
	 */
	private String situationId;

	private List<Effect> effects = new ArrayList<Effect>();

	@Override
	public void select() {
		ScriptingService scriptingService = GameBeansContext
				.getBean(ScriptingService.class);
		for (Effect effect : effects) {
			if (effect.getCondition() == null
					|| scriptingService.conditionIsMet(effect.getCondition())) {
				scriptingService.execute(effect.getEffect());
			}
		}

		super.select();

	}

	@XmlAttribute
	public String getRef() {
		return ref;
	}

	public void setRef(String ref) {
		this.ref = ref;
	}

	@XmlAttribute
	public String getSee() {
		return see;
	}

	public void setSee(String see) {
		this.see = see;
	}

	public String getSituationId() {
		return situationId;
	}

	public void setSituationId(String situationId) {
		this.situationId = situationId;
	}

	@XmlElement(name = "effect")
	public List<Effect> getEffects() {
		return effects;
	}

	public void setEffects(List<Effect> effects) {
		this.effects = effects;
	}

}
